# +----------------------------------------------------------------------
# | Pink [ A modern python web framework ]
# +----------------------------------------------------------------------
# | Copyright (c) 2023 http:#unnnnn.com All rights reserved.
# +----------------------------------------------------------------------
# | Author: chenjianhua <unnnnn@foxmail.com>
# +----------------------------------------------------------------------

"""Queue Retry Command."""
from .Command import Command
from cleo.io.inputs.option import Option as option


class QueueRetryCommand(Command):
    name = 'queue:retry'
    description = 'Puts all failed queue jobs back onto the queue.'
    options = [
        option('connection', 'c', description='The connection to the job queue.'),
        option('queue', 'q', description='The queue to send the job to.'),
        option('driver', 'd', description='Specify the driver you would like to connect to.'),
    ]

    """
    Puts all failed queue jobs back onto the queue.

    queue:retry
        {--c|--connection=default : Specifies the database connection if using database driver.}
        {--queue=default : The queue to listen to}
        {--d|driver=None : Specify the driver you would like to connect to}
    """

    def __init__(self, application):
        super().__init__()
        self.app = application

    def handle(self):
        driver = None if self.option("driver") == "None" else self.option("driver")

        return self.app.make("queue").retry(
            {
                "driver": driver,
                "connection": self.option("connection"),
                "queue": self.option("queue"),
            }
        )
